﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_PromotionPolicy")]
    public class P_PromotionPolicy : IEntity<P_PromotionPolicy>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime EndDate { get; set; }
        public bool Enable { get; set; }
        public int ActiveCode { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string AttachPolicy { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }
        public int Range { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Summary { get; set; }

        public override P_PromotionPolicy Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_PromotionPolicy Find(System.Linq.Expressions.Expression<Func<P_PromotionPolicy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PromotionPolicy.FirstOrDefault(where);
                if (item != null)
                {
                    this.ActiveCode = item.ActiveCode;
                    this.AttachPolicy = item.AttachPolicy;
                    this.BeginDate = item.BeginDate;
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    this.Enable = item.Enable;
                    this.EndDate = item.EndDate;
                    this.ID = item.ID;
                    this.Range = item.Range;
                    this.Summary = item.Summary;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_PromotionPolicy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PromotionPolicy.Any(where);
            }
        }
    }
}